/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.app.controller.system;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysNotice;
import com.easy.module.system.query.SysNoticeQuery;
import com.easy.module.system.service.ISysNoticeService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 通知公告表 控制器
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-21
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("system/notice")
public class SysNoticeController extends BaseController
{

    final ISysNoticeService sysNoticeService;

    /**
    * 获取通知公告表列表
    */
    @PreAuthorize("@perm.hasPermission('system:notice$list')")
    @GetMapping("/page")
    public R<Pagination<SysNotice>> page(SysNoticeQuery query) {
        return R.success(sysNoticeService.page(query));
    }

    @Log(title = "通知公告管理", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:notice$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNoticeQuery query) throws IOException {
        List<SysNotice> list = sysNoticeService.list(query);
        ExcelUtils.exportExcel(response, list, "通知公告表数据");
    }

    /**
    * 根据通知公告表编号获取详细信息
    */
    @PreAuthorize("@perm.hasPermission('system:notice$query')")
    @GetMapping(value = "/{id}")
    public R<SysNotice> get(@PathVariable("id") Long id) {
        return R.success(sysNoticeService.getById(id));
    }

    /**
    * 新增
    */
    @PreAuthorize("@perm.hasPermission('system:notice$add')")
    @Log(title = "通知公告管理", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysNotice model) {
        return R.result(sysNoticeService.save(model));
    }

    /**
    * 修改
    */
    @PreAuthorize("@perm.hasPermission('system:notice$edit')")
    @Log(title = "通知公告管理", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysNotice model) {
        return R.result(sysNoticeService.updateById(model));
    }

    /**
    * 删除
    */
    @PreAuthorize("@perm.hasPermission('system:notice$remove')")
    @Log(title = "通知公告管理", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids) {
        return R.result(sysNoticeService.removeBatchByIds(List.of(ids)));
    }

}